import {
    _decorator,
    Camera,
    Color,
    Component,
    instantiate,
    Label,
    Node,
    Prefab,
    ResolutionPolicy,
    resources,
    Sprite,
    tween,
    UIOpacity,
    view
} from 'cc';
import {WorldPubSubServer} from "db://assets/script/common/pubsub/world-pub-sub-server";
import {configsHelper} from "db://assets/script/configs-helper";
import {join} from "lodash";
import Long from "long";

const {ccclass, property} = _decorator;

@ccclass('lobby')
export class lobby extends Component {

    @property(Node)
    map: Node = null!;

    @property(Node)
    city: Node = null!;

    @property(Node)
    gui: Node = null!;

    @property(Node)
    toast: Node = null!;

    @property(Node)
    alert: Node = null!;

    @property(Camera)
    camera: Camera = null!;

    @property(Camera)
    uiCamera: Camera = null!;

    @property(Node)
    changeMask!: Node;

    @property(Prefab)
    clickMaskPrefab!: Prefab;

    @property(WorldPubSubServer)
    worldPubSubServer: WorldPubSubServer = null!;

    tempNodes!: Node
    loginUiTemp: Node | null = null!;

    latestMaskTouch: number = 0
    static instance: lobby = null!;

    status: number = 1 // 1 login; 2 main

    // 设置你的设计分辨率，例如 16:9
    private readonly designResolution = {width: 720, height: 1280};

    // private readonly designResolution = {width: 1280, height: 720};

    start() {
        lobby.instance = this;
        this.change2Login()
        this.uiCamera.orthoHeight = 640
        console.log(this.camera)
        console.log(this.uiCamera)
    }

    change2Main(): void {
        if (this.status == 2) return
        this.status = 2;
        this.showChangeMask(() => {
            this.loginUiTemp!.destroy()
            const prefab = resources.get("prefab/mainui", Prefab)!
            const mainUI = instantiate(prefab)
            // const tempBoxPrefab = resources.get("prefab/ui-temp-box", Prefab)!
            // const uiTempBox = instantiate(tempBoxPrefab)
            this.gui.addChild(mainUI)
            this.tempNodes = mainUI.getChildByName('ui-temp-box')!
        })

    }


    change2Login(): void {
        this.status = 1
        const prefab = resources.get("prefab/common/login-ui", Prefab)!
        const loginUI = instantiate(prefab)
        this.gui.addChild(loginUI)
        this.loginUiTemp = loginUI
    }

    addUI(path: string) {
        let prefab = resources.get(path, Prefab)!
        let ui = instantiate(prefab)
        this.tempNodes.addChild(ui)
        return ui
    }

    getMapCamera() {
        return this.camera
    }

    resetMapCameraHeight() {
        this.camera!.orthoHeight = 640
    }

    showToast(str: string, time = 2) {
        let prefab = resources.get("prefab/toast", Prefab)!
        let toast = instantiate(prefab)
        this.toast.getChildByName("Layout")!.addChild(toast);
        toast.getChildByName("Label")!.getComponent(Label)!.string = str;
        tween(toast.getComponent(UIOpacity)!)
            .to(time, {opacity: 0})
            .call(() => {
                toast.destroy()
            })
            .start()
    }

    showAssetGainToast(assets: { [key: number]: Long }) {
        const assetTips: string[] = []
        for (let assetId in assets) {
            const ac = configsHelper.loadAssetConfig(parseInt(assetId))
            if (ac) {
                assetTips.push(ac.name + ' x ' + Math.floor(assets[assetId].toNumber()))
            }
        }
        if (assetTips.length > 0) {
            this.showToast('获得：' + assetTips.join(","))
        }
    }

    showAssetGainByMapToast(assets: Map<number, number>) {
        const assetTips: string[] = []
        for (let [assetId, assetNum] of assets) {
            const ac = configsHelper.loadAssetConfig(assetId)
            if (ac) {
                assetTips.push(ac.name + ' x ' + Math.floor(assetNum))
            }
        }
        if (assetTips.length > 0) {
            this.showToast('获得：' + assetTips.join(","))
        }
    }

    showChangeMask(callback: () => void) {
        tween(this.changeMask.getComponent(UIOpacity)!)
            // .to(0.1, {opacity: 255})
            .to(0.4, {opacity: 255})
            .call(() => callback())
            .to(0.3, {opacity: 0})
            .tag(9000)
            .start()
    }

    addUIWithClickMask(path: string, opacity: number, clickCallback: (node: Node) => void): Node {
        const prefab = resources.get(path, Prefab)!
        const ui = instantiate(prefab)
        const clickMask = instantiate(this.clickMaskPrefab)
        clickMask.on(Node.EventType.TOUCH_START, () => {
            this.latestMaskTouch = new Date().getTime()
        })
        clickMask.on(Node.EventType.TOUCH_END, () => {
            if (this.checkShortTouch()) {
                clickCallback(ui)
                clickMask.destroy()
            }
        })
        ui.on(Node.EventType.NODE_DESTROYED, () => {
            clickMask.destroy()
        })
        clickMask.getComponent(UIOpacity)!.opacity = opacity
        this.tempNodes.addChild(clickMask)
        this.tempNodes.addChild(ui)
        return ui
    }

    showClickMask(opacity: number, clickCallback: () => void) {
        const clickMask = instantiate(this.clickMaskPrefab)
        clickMask.on(Node.EventType.TOUCH_START, () => {
            this.latestMaskTouch = new Date().getTime()
        })
        clickMask.on(Node.EventType.TOUCH_END, () => {
            if (this.checkShortTouch()) {
                clickCallback()
                clickMask.destroy()
            }
        })
        clickMask.getComponent(UIOpacity)!.opacity = opacity
        this.tempNodes.addChild(clickMask)
    }

    showClickMaskWithNode(node: Node, opacity: number, clickCallback: () => void) {
        const clickMask = instantiate(this.clickMaskPrefab)
        clickMask.on(Node.EventType.TOUCH_START, () => {
            this.latestMaskTouch = new Date().getTime()
        })
        clickMask.on(Node.EventType.TOUCH_END, () => {
            if (this.checkShortTouch()) {
                clickCallback()
                clickMask.destroy()
            }
        })
        clickMask.getComponent(UIOpacity)!.opacity = opacity
        if (node.parent) {
            clickMask.layer = node.parent.layer
            node.parent.insertChild(clickMask, node.getSiblingIndex() - 1)
        } else {
            this.tempNodes.addChild(clickMask)
        }
        node.on(Node.EventType.NODE_DESTROYED, () => {
            clickMask.destroy()
        })
    }

    checkShortTouch() {
        const rst = this.latestMaskTouch == -1 || new Date().getTime() - this.latestMaskTouch <= 200
        this.latestMaskTouch = -1
        return rst
    }

    clearAllTempUI() {
        if (this.tempNodes) this.tempNodes.destroyAllChildren()
    }

    update(deltaTime: number) {
    }
}


